package org.exhaust.framework.net.cmpp20.protocol;

import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.exhaust.framework.net.cmpp20.protocol.client.*;
import org.exhaust.framework.net.cmpp20.protocol.server.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class CMPPCommandHelper {
    private static final Map<Long,Class<? extends CMPPBody>> COMMAND_REGISTRY = new ConcurrentHashMap<>(32);

    static {
        COMMAND_REGISTRY.put(0x00000001L, CMPPConnect.class);
        COMMAND_REGISTRY.put(0x80000001L, CMPPConnectResp.class);

        COMMAND_REGISTRY.put(0x00000002L, CMPPTerminate.class);
        COMMAND_REGISTRY.put(0x80000002L, CMPPTerminateResp.class);

        COMMAND_REGISTRY.put(0x00000004L, CMPPSubmit.class);
        COMMAND_REGISTRY.put(0x80000004L, CMPPSubmitResp.class);

        COMMAND_REGISTRY.put(0x00000005L, CMPPDeliver.class);
        COMMAND_REGISTRY.put(0x80000005L, CMPPDeliverResp.class);

        COMMAND_REGISTRY.put(0x00000006L, CMPPQuery.class);
        COMMAND_REGISTRY.put(0x80000006L, CMPPQueryResp.class);

        COMMAND_REGISTRY.put(0x00000008L, CMPPActiveTest.class);
        COMMAND_REGISTRY.put(0x80000008L, CMPPActiveTestResp.class);
    }

    public static CMPPBody createBody(long commandId){
        Class<? extends CMPPBody> clazz = COMMAND_REGISTRY.get(commandId);
        if(clazz == null){
            return null;
        }
        try{
            CMPPBody cmppBody = clazz.newInstance();
            return cmppBody;
        }catch (Exception e){
            log.error("创建CMPPBODY失败",e);
            return null;
        }
    }

}
